﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using kxcxw.Data.Entities;
using kxcxw.Data;
using Webdiyer.WebControls.Mvc;
using kxcxw.Data.Common;
using System.Web.Security;

namespace KXCXW.Web.Areas.Admin.Controllers
{
    public class AnnouncementInfoController : Controller
    {
        private KXCXWDBContext db = new KXCXWDBContext();

        //
        // GET: /AnnouncementInfo/
        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Index(string sortOrder, int? page = 1)
        {
            ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
            var announcementinfo = from s in db.AnnouncementInfoes
                                   select s;
            switch (sortOrder)
            {
                case "Date desc":
                    announcementinfo = announcementinfo.OrderByDescending(s => s.CreateDate);
                    break;
                default:
                    announcementinfo = announcementinfo.OrderBy(s => s.CreateDate);
                    break;
            }
            int pageSize = 20;
            int pageIndex = page ?? 1;
            return View(announcementinfo.ToPagedList(pageIndex, pageSize));
        }

        //
        // GET: /AnnouncementInfo/Details/5
        [Authorize(Roles = DataConstants.Admin)]
        public ViewResult Details(string id)
        {
            AnnouncementInfo announcementinfo = db.AnnouncementInfoes.Find(id);

            SysUserInfo sysuserinfo = (from q in db.SysUserInfoes
                                       where q.UserID == announcementinfo.CreatePersonID
                                       select q).FirstOrDefault();
            announcementinfo.CreatePersonID = sysuserinfo.UserName;

            SysItemDetial sid = (from q in db.SysItemDetials
                                 where q.ItemDeitalID == announcementinfo.ItemID
                                 select q).FirstOrDefault();
            announcementinfo.ItemID = sid.itemDetialName;

            return View(announcementinfo);
        }

        //
        // GET: /AnnouncementInfo/Create
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Create()
        {
            ViewBag.AnnouncementID = new SelectList(db.AnnouncementInfoes, "AnnouncementID", "AnnouncementID");
            List<SysItemDetial> itemlist = db.SysItemDetials.Where(q => q.ItemID ==DataConstants.AnnouncementTypeItemID).ToList<SysItemDetial>();
            var sl = from c in itemlist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.itemTypelist = new SelectList(sl, "Id", "Name");
            return View();
        }

        //
        // POST: /AnnouncementInfo/Create
        [Authorize(Roles = DataConstants.Admin)]
        [HttpPost]
        public ActionResult Create(AnnouncementInfo announcementinfo)
        {
            if (ModelState.IsValid)
            {
                MembershipUser user = Membership.GetUser();
                announcementinfo.AnnouncementID = Guid.NewGuid().ToString();
                announcementinfo.CreateDate = DateTime.Now;
                announcementinfo.CreatePersonID = user.ProviderUserKey.ToString();
                db.AnnouncementInfoes.Add(announcementinfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(announcementinfo);
        }

        //
        // GET: /AnnouncementInfo/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Edit(string id)
        {
            AnnouncementInfo announcementinfo = db.AnnouncementInfoes.Find(id);
            ViewBag.AnnouncementID = new SelectList(db.AnnouncementInfoes, "AnnouncementID", "AnnouncementID", announcementinfo.AnnouncementID);

            List<SysItemDetial> itemlist = db.SysItemDetials.Where(q => q.ItemID == DataConstants.AnnouncementTypeItemID).ToList<SysItemDetial>();
            var sl = from c in itemlist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.itemTypelist = new SelectList(sl, "Id", "Name");
            return View(announcementinfo);
        }

        //
        // POST: /AnnouncementInfo/Edit/5
        [Authorize(Roles = DataConstants.Admin)]
        [HttpPost]
        public ActionResult Edit(AnnouncementInfo announcementinfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(announcementinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.AnnouncementID = new SelectList(db.AnnouncementInfoes, "AnnouncementID", "AnnouncementID", announcementinfo.AnnouncementID);
            List<SysItemDetial> itemlist = db.SysItemDetials.Where(q => q.ItemID == "52FF4ABB-CE8E-44C8-AF97-87D811A99866").ToList<SysItemDetial>();
            var sl = from c in itemlist
                     select new
                     {
                         Name = c.itemDetialName,//值
                         Id = c.ItemDeitalID,//健
                     };
            ViewBag.itemTypelist = new SelectList(sl, "Id", "Name", announcementinfo.ItemID);
            return View(announcementinfo);
        }

        //
        // GET: /AnnouncementInfo/Delete/5
        [Authorize(Roles = DataConstants.Admin)]
        public ActionResult Delete(string id)
        {
            AnnouncementInfo announcementinfo = db.AnnouncementInfoes.Find(id);
            db.AnnouncementInfoes.Remove(announcementinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}